/*
 * @Author: ZhangXiaodong
 * @Date: 2025-04-15 23:48:53
 * @LastEditors: ZhangXiaodong
 * @LastEditTime: 2025-04-26 00:09:31
 * @Description: 创建博客
 * @FilePath: /blog-ost/src/controller/blog/create.js
 */
const mysqlPool = require("../../db/mysql/db.pool");

/**
 * 创建博客的函数
 * @param {Object} body - 博客的相关信息
 * @param {string} body.title - 博客标题
 * @param {string} body.author - 博客作者
 * @param {string} body.content - 博客内容
 * @param {number} body.userId - 用户 ID
 * @returns {Promise<Object>} - 包含新博客 ID 的对象
 * @throws {Error} - 如果参数验证失败或数据库操作出错
 */

const BODY_LAYOUT = {
  title: "", author: "", content: "", userId: null 
  
}


const create = async (body = BODY_LAYOUT) => {
  const { title, author, content, userId } = body;
  // 参数验证
  if (!title || !author || !content || !userId) {
    throw new Error('创建博客所需的参数不完整，请提供 title、author、content 和 userId');
  }

  const SQL = `INSERT INTO blogs (title,author,content,userId) values (?,?,?,?)`;
  try {
    const result = await mysqlPool.exec(SQL, [title, author, content, userId]);
    return {
      id: result.insertId,
    };
  } catch (err) {
    console.error('创建博客时数据库操作出错:', err);
    throw err;
  }
};

module.exports = create;
